<?php

namespace App\Repository;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalCommentConst;
use App\Consts\GlobalConst;
use App\Models\IndexComment;
use App\Models\Select\CommentSelect;
use App\Models\SysComment;
use App\Models\UserMessageCommentSend;

class CommentRepository extends BaseRepository
{
    /**
     * 获取评论列表
     * @param $searchParams
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getCommentList($searchParams)
    {
        $provider = SysComment::getInstance($searchParams['target_id']);
        $provider = $provider->where('target_id', $searchParams['target_id']);

        if (array_key_exists('is_deleted', $searchParams)) {
            $provider->where('is_deleted', $searchParams['is_deleted']);
        }

        if (array_key_exists('is_view', $searchParams)) {
            $provider->whereIn('is_deleted', [GlobalConst::IS_NOT_DELETE, GlobalConst::IS_DELETED]);
            $provider->where('is_view', $searchParams['is_view']);
        }

        $provider->orderBy('created_at', GlobalConst::ORDER_TYPE_DESC);
        if (isset($searchParams['order'])) {
            if ($searchParams['order'] == GlobalCommentConst::ORDER_HOT) {
                $provider->orderBy('hot', GlobalConst::ORDER_TYPE_DESC);
            }
            if ($searchParams['order'] == GlobalCommentConst::ORDER_NOTE_USER) {
                $provider->where('have_author_reply', GlobalCommentConst::HAVE_AUTHOR_REPLY);
            }
        }

        return $this->setPagination($provider);
    }

    /**
     * 新增评论
     * @param $loadData
     * @return SysComment|\Illuminate\Database\Eloquent\Builder|SysComment
     */
    public function addComment($loadData)
    {
        return SysComment::getModel($loadData['target_id'])->create($loadData);
    }

    /**
     * 【暂时】新增索引表记录
     * @param $loadData
     * @return IndexComment|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     */
    public function addIndexComment($loadData)
    {
        return IndexComment::getModel()->create($loadData);
    }

    /**
     * 删除评论
     * @param SysComment $comment
     * @return SysComment
     */
    public function delComment($comment): bool
    {
        return $comment
            ->update(['is_deleted' => GlobalConst::IS_DELETED]);
    }

    /**
     * 【暂时】删除索引表记录
     * @param SysComment $comment
     * @return bool
     */
    public function delIndexComment($comment): bool
    {
        return IndexComment::getInstance()
            ->where('comment_id', $comment)
            ->where('type', GlobalConst::TARGET_TYPE_COMMENT)
            ->update(['is_deleted' => GlobalConst::IS_DELETED]);
    }

    /**
     * 更新评论信息
     * @param SysComment $comment
     * @param $loadData
     * @return mixed
     */
    public function editComment($loadData)
    {
        $comment = $this->getComment($loadData['comment_id']);
        if (!$comment) {
            return false;
        }

        return $comment
            ->where('comment_id', $loadData['comment_id'])
            ->update($loadData);
    }

    /**
     * 【暂时】更新索引表记录
     * @param $condition
     * @param $loadData
     * @return int
     */
    public function editIndexComment($condition, $loadData)
    {
        return IndexComment::getInstance()
            ->where($condition)
            ->update($loadData);
    }

    /**
     * 通过主体ID找到评论
     * @param $targetId
     * @param $commentId
     * @param string $selectScene
     * @return SysComment|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getCommentByTargetId($targetId, $commentId, $selectScene = CommentSelect::SELF_DETAIL)
    {
        return SysComment::getModel($targetId)
            ->select(CommentSelect::getSelects($selectScene))
            ->where(['comment_id' => $commentId])
            ->first();
    }

    /**
     * 通过主体ID和评论ID找到评论
     * @param $commentId
     * @param string $selectScene
     * @return SysComment|\Illuminate\Database\Eloquent\Builder|null
     */
    public function getComment($commentId, $selectScene = CommentSelect::SELF_DETAIL)
    {
        $indexComment = IndexComment::getModel()->select(['target_id'])
            ->where('comment_id', $commentId)
            ->first();
        if (!$indexComment) {
            return null;
        }
        return SysComment::getModel($indexComment->target_id)
            ->select(CommentSelect::getSelects($selectScene))
            ->where(['comment_id' => $commentId])
            ->first();
    }

    /**
     * 通过主体用户ID和主体ID删除关注记录
     *
     * 该用户所有的粉丝的[user_attention_]记录均删除
     * @param $noteId
     * @param $noteUserId
     * @return bool
     */
    public static function deleteByNote($noteId, $noteUserId): bool
    {
        $indexComments = IndexComment::getModel()
            ->where('target_id', $noteId)
            ->where('type', GlobalConst::TARGET_TYPE_COMMENT)
            ->where('user_id', '<>', $noteUserId)
            ->get();
        $userIdArr = ArrayHelper::getColumn($indexComments, 'user_id');
        foreach ($userIdArr as $userId) {
            UserMessageCommentSend::getModel($userId)
                ->where('sender_id', $userId)
                ->where('target_id', $noteId)
                ->update(['is_deleted' => GlobalConst::IS_DELETED]);
        }

        return true;
    }
}
